package com.simpleplayer.views.videoview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.IntDef;

import com.simpleplayer.MediaPlayer.SimplePlayer;
import com.simpleplayer.services.LogUtil;

import java.util.HashMap;
import java.util.Map;

@SuppressLint("ViewConstructor")
public class VideoView extends FrameLayout {

    private static final String TAG = "VideoView";

    @IntDef({UNDEFINEMODE, INLINE, FLOATWINDOW, FULLSCREEN})
    public @interface DISPLAYMODE {}

    public static final int UNDEFINEMODE = 0;
    public static final int INLINE = 1;
    public static final int FLOATWINDOW = 2;
    public static final int FULLSCREEN = 3;

    private final Context mContext;
    private final SimplePlayer mPlayer;
    private final Map<Integer, VideoViewInternal> mVideoViews = new HashMap<Integer, VideoViewInternal>();
    private View mVideoContentView;

    @DISPLAYMODE
    private int mCurrentDisplayMode = UNDEFINEMODE;

    public VideoView(Context context, SimplePlayer player, @DISPLAYMODE int defaultMode) {
        super(context);
        setLayoutParams(
                new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT
                )
        );

        mContext = context;
        mPlayer = player;
        switchDisplayModeTo(defaultMode);
    }

    public void setContentView(View contentView) {
        mVideoContentView = contentView;
        getVideoView(mCurrentDisplayMode).setContentView(mVideoContentView);
    }

    public int getCurrentDisPlayMode() {
        return mCurrentDisplayMode;
    }

    public void switchDisplayModeTo(int newDisplayMode) {
        if (mCurrentDisplayMode == newDisplayMode) return;

        // 调整布局方向
        if (newDisplayMode == FULLSCREEN) {
            if (mContext instanceof Activity) {
                ((Activity)mContext).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            }
        } else if (newDisplayMode == INLINE) {
            if (mContext instanceof Activity) {
                ((Activity)mContext).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
            }
        }

        onHide();
        mCurrentDisplayMode = newDisplayMode;
        onShow();
    }

    private void onHide() {
        getVideoView(mCurrentDisplayMode).onHide();
        if (getChildCount() != 0) {
            removeAllViews();
        }
    }

    private void onShow() {
        getVideoView(mCurrentDisplayMode).setContentView(mVideoContentView);
        if (mCurrentDisplayMode != FLOATWINDOW) {
            addView(getVideoView(mCurrentDisplayMode));
        }
        getVideoView(mCurrentDisplayMode).onShow();
    }

    private VideoViewInternal getVideoView(@DISPLAYMODE int mode) {
        VideoViewInternal view = mVideoViews.get(mode);

        if (view == null) {
            view = createVideoView(mode);
            mVideoViews.put(mode, view);
        }
        return view;
    }

    private VideoViewInternal createVideoView(@DISPLAYMODE int mode) {
        switch (mode) {
            case UNDEFINEMODE:
                return new MockVideoView(mContext);
            case INLINE:
                return new InlineVideoView(mContext, mPlayer, this);
            case FULLSCREEN:
                return new FullScreenVideoView(mContext, mPlayer, this);
            case FLOATWINDOW:
                return new FloatWindowVideoView(mContext, mPlayer, this);
            default:
                LogUtil.w(TAG, "create video error, mode = " + mode);
                return new MockVideoView(mContext);
        }
    }

    @Override
    public void onLayout(boolean changed, int left, int top, int right, int bottom) {
        // 使用基类更新所有子View的布局
        super.onLayout(changed, left, top, right, bottom);
    }
}
